﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Maticsoft.DBUtility;
namespace DAL
{
    //Customer
    public partial class CustomerDAL
    {

        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Customer");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        public bool Authenticated(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Customer");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id and Status=1 ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool Exists(String Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Customer");
            strSql.Append(" where ");
            strSql.Append(" name = @name  ");
            SqlParameter[] parameters = {
					new SqlParameter("@name",  SqlDbType.NVarChar,50)
};
            parameters[0].Value = Name;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public int Login(String Name,String Password)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id from Customer");
            strSql.Append(" where ");
            strSql.Append(" name = @name and Password=@Password ");
            SqlParameter[] parameters = {
					new SqlParameter("@name",  SqlDbType.NVarChar,50),
                    new SqlParameter("@Password",  SqlDbType.NVarChar,255)
                   };
            parameters[0].Value = Name;
            parameters[1].Value = Password;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            return (int)obj;
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Common.Model.Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Customer(");
            strSql.Append("name,Password,Nick,CreateTime,Email,CellPhone,Status");
            strSql.Append(") values (");
            strSql.Append("@name,@Password,@Nick,@CreateTime,@Email,@CellPhone,@Status");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Password", SqlDbType.NVarChar,255) ,            
                        new SqlParameter("@Nick", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Email", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CellPhone", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.name;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.Nick;
            parameters[3].Value = model.CreateTime;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.CellPhone;
            parameters[6].Value = model.Status;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Common.Model.Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Customer set ");

            strSql.Append(" name = @name , ");
            strSql.Append(" Password = @Password , ");
            strSql.Append(" Nick = @Nick , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" Email = @Email , ");
            strSql.Append(" CellPhone = @CellPhone , ");
            strSql.Append(" Status = @Status  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Password", SqlDbType.NVarChar,255) ,            
                        new SqlParameter("@Nick", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Email", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CellPhone", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.name;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.Nick;
            parameters[4].Value = model.CreateTime;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.CellPhone;
            parameters[7].Value = model.Status;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Customer ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除几条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Customer ");
            strSql.Append(" where ID in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Common.Model.Customer GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, name, Password, Nick, CreateTime, Email, CellPhone, Status  ");
            strSql.Append("  from Customer ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;


            Common.Model.Customer model = new Common.Model.Customer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.name = ds.Tables[0].Rows[0]["name"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.Nick = ds.Tables[0].Rows[0]["Nick"].ToString();
                if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                model.CellPhone = ds.Tables[0].Rows[0]["CellPhone"].ToString();
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


    }
}

